
package com.runa.monitor.platinterface.netservice.equipment.dtu;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Node;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.monitor.platinterface.netservice.equipment.dtu.point.FourGValvePoints;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.dto.ModbusResponseData;
import com.runa.protocol.dto.ResponseContent;
import com.runa.rccprotocol.netservice.dtu.ModbusRtuResponse;
import com.runa.rccprotocol.netservice.modbus.ModbusRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;

/**
 * 2024/9/2 20:23
 * zhangYB
 */
@Slf4j
public class FourGDtuTaskFunction extends DtuTaskFunction {

    protected FourGValvePoints fourGValvePoints;

    private String task;

    private String holdInfo;

    private int priority;


    public FourGDtuTaskFunction(TaskFunction function, Node node, Integer count) {
        super(function, node, count);
    }

    public FourGDtuTaskFunction(TaskFunction function, Node node, Integer count, boolean read) {
        super(function, node, count, read);
    }

    public FourGDtuTaskFunction(String task, String holdInfo, int priority, TaskFunction function, Node node, Integer count, boolean read) {
        super(function, node, count, read);
        this.task = task;
        this.holdInfo = holdInfo;
        this.priority = priority;
    }

    public void setFourGValvePoints(FourGValvePoints fourGValvePoints) {
        this.fourGValvePoints = fourGValvePoints;
    }

    @Override
    public void addResponse(ModbusRtuResponse response) {
        if (requests.size() == (responses.size() + 1) && getSerial().equals(response.getAddress())) {
            ModbusResponseData modbusResponse = response.getModbusResponse();
            ModbusRequest modbusRequest = getLastRequest();
            if (modbusResponse.getCount() != modbusRequest.getRegisterCount()*2) {
                log.debug("{} 请求与响应设备寄存器个数不同", getSerial());
                return;
            }
            response.setModbusRequest(modbusRequest);
            responses.add(response);
        }
    }


    @Override
    public void ok(ResponseContent responseContent) {
        if (null != node) {
            responseContent.setIndexNo(node.getIndex());
            responseContent.setId(node.getId());
            responseContent.setSerial(node.getSerial());
        }
        if (null != fourGValvePoints) {
            fourGValvePoints.handle(responseContent);
        }
        List<ValveVO> valveVOList = Collections.singletonList(responseContent.getValve());
        LogUtil.info(getSerial(), "FourGDtuTaskFunction.ok: {}", JSON.toJSONString(valveVOList));
        function.doApply(CommStatus.OK, valveVOList);
    }

    @Override
    public String getSerial() {
        return node.getSerial();
    }

    public String getTopic() {
        return "/RU-VC-05/" + node.getSerial() + "/down";
    }

}
